home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*________________________________________________________________________
- |
- | blixtexture.c - texture initialization and reading
- |
- |
- | 1994 Frans van Hoesel, Xtreme Graphics Software
- |
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <malloc.h>
- #include <alloca.h>
- #include <gl/gl.h>
- #include <gl/device.h>
- #include <gl/image.h>
-
- #include "blix.h"
- #include "blixtexture.h"
- #include "blixsound.h"
-
- /* from imagelib: */
- IMAGE *iopen();
- void getrow(IMAGE *, unsigned short *, int , int);
- void iclose(IMAGE *);
-
- static void readtexture(char *name, long num, int colorflag);
- static unsigned long *readrgbimage(char *name);
- static unsigned long *readbwimage(char *name);
-
- #define RGB 1
- #define BW 0
-
- void init_textures(void) {
- readtexture("walls.rgb", 2, RGB);
- /* readtexture("sphere.bw", 3, BW); */
- }
-
- static void readtexture(char *name, long num, int colorflag) {
- unsigned long *data;
-
- static float texps[] = {
- TX_MINFILTER, TX_MIPMAP_BILINEAR,
- TX_MAGFILTER, TX_BILINEAR,
- TX_WRAP, TX_REPEAT,
- TX_NULL};
- static float tevps[] = {TV_NULL };
-
- if (colorflag == RGB) {
- data = readrgbimage(name);
- texdef2d(num, 3, 128, 128, data, 0, texps);
- } else {
- data = readbwimage(name);
- texdef2d(num, 1, 128, 128, data, 0, texps);
- }
- tevdef(1, 0, tevps);
- free(data);
- }
-
- static unsigned long *readbwimage(char *filename) {
- unsigned long *base, *lptr;
- unsigned short *buf;
- unsigned char *cptr;
- IMAGE *image;
- int x,y;
- char fullpath[512];
- int newx;
-
- strcpy(fullpath, datadir);
- strcat(fullpath, filename);
-
- image = iopen(fullpath,"r");
- if (image == NULL) {
- fprintf(stderr, "%s: cannot open file '%s'\n", basename, filename);
- end_sound();
- gexit();
- exit (1);
- }
- if (image->zsize != 1) {
- iclose(image);
- fprintf(stderr,"%s: texture file '%s' is not in BW format\n",
- basename, filename);
- end_sound();
- gexit();
- exit(1);
- }
- newx = image->xsize / 4;
- base = (unsigned long *)malloc(newx*image->ysize*sizeof(long));
- buf = (unsigned short *)alloca(image->xsize*sizeof(short));
- lptr = base;
- for(y=0; y<image->ysize; y++) {
- cptr = (unsigned char *) lptr;
- getrow(image,buf,y,0);
- for (x=0; x<image->xsize; x++) {
- *cptr++ = buf[x];
- }
- lptr += newx;
- }
- iclose(image);
- return base;
- }
-
- static unsigned long *readrgbimage(char *filename) {
- unsigned long *base, *lptr;
- unsigned short *rbuf, *gbuf, *bbuf;
- IMAGE *image;
- int newx;
- unsigned char *cptr;
- int x, y;
- char fullpath[512];
-
- strcpy(fullpath, datadir);
- strcat(fullpath, filename);
-
- image = iopen(fullpath,"r");
- if (image == NULL) {
- fprintf(stderr, "%s: cannot open file '%s'\n", basename, filename);
- end_sound();
- gexit();
- exit(1);
- }
- if (image->zsize != 3) {
- iclose(image);
- fprintf(stderr,"%s: texture file '%s' is not in RGB format\n",
- basename, filename);
- end_sound();
- gexit();
- exit(1);
- }
-
- newx = image->xsize * 3 ;
- newx = newx / 4;
- base = (unsigned long *)malloc(newx*image->ysize*sizeof(long));
- rbuf = (unsigned short *)alloca(image->xsize*sizeof(short));
- gbuf = (unsigned short *)alloca(image->xsize*sizeof(short));
- bbuf = (unsigned short *)alloca(image->xsize*sizeof(short));
- if(base == NULL || rbuf == NULL || gbuf == NULL || bbuf == NULL) {
- fprintf(stderr, "%s: not enough memory\n", basename);
- end_sound();
- gexit();
- exit (1);
- }
- lptr = base;
- for(y=0; y<image->ysize; y++) {
- cptr = (unsigned char *) lptr;
- getrow(image,rbuf,y,0);
- getrow(image,gbuf,y,1);
- getrow(image,bbuf,y,2);
- for (x=0; x < image->xsize; x++) {
- *cptr++ = bbuf[x];
- *cptr++ = gbuf[x];
- *cptr++ = rbuf[x];
- }
- lptr += newx;
- }
- iclose(image);
- return base;
- }
-
-